<?php

namespace Blog\Controller;

use Common\Controller\AddonController;
use Org\Util\ArrayToolkit;

class ManageController extends AddonController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function add()
    {
        $id = I('id', 0, 'intval');
        $model = D('Article');
        if ($id) {
            $info = $model->find($id);
            if (!$info) {
                $this->error('文章不存在');
            }
        }
        $this->IS_POST(function () use ($model, $id) {
            $data = $model->create();
            if (!$data) {
                $this->error($model->getError());
            }
            $data['author'] = $this->_me['id'];
            if ($model->in2replace($data, ['id' => $id]) !== false) {
                if (!$id) {
                    $url = $_SERVER['HTTP_REFERER'];
                }
                $this->success('保存成功', $url);
            }
            $this->error('保存失败');
        });
        $cate = D('Tree', 'Logic')->toTree(D('Category')->getChilds(C('category_code')));
        $cate = ArrayToolkit::KeyAndValue($cate, 'id', 'title');
        $status = [1 => '公开', -1 => '草稿', -2 => '私密',];
        $rec = ['否', '是',];
        $this->_web('title', '新建文章');
        $this->_web('panel_title', '新建文章');
        if ($info) {
            $this->_web('breadcrumb', [
                ['title' => '文章列表', 'url' => U('post_list'),],
                ['title' => '编辑文章', 'url' => U('')],
            ]);
            $this->_web('title', '编辑文章');
            $this->_web('panel_title', '编辑文章');
        }
        $this->assign('info', $info);
        $this->assign('status', $status);
        $this->assign('rec', $rec);
        $this->assign('cate', $cate);
        $this->display();
    }

    public function post_list()
    {

        $this->IS_POST(function () {
            $model = D('Article');
            $keyword = '%' . str_replace(' ', '%', I('keyword')) . '%';
            $map = [
                'title|content' => ['like', $keyword]
            ];
            $data = $model->where($map)->order('create_time desc')->findPage();
            foreach ($data['data'] as &$row) {
                $row['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            }
            $this->ajaxReturn($data);
        });
        $this->_web('title', '文章管理');
        $this->_web('panel_title', '文章管理');
        $listData = array(
            'url' => U(''),
            'toolbar' => '#barDemo',
            'toolbarTpl' => 'toolbar',
            'page' => true,
        );
        $listData['cols'][] = array('type' => 'checkbox', 'fixed' => 'left');
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'title', 'title' => '文章标题', 'width' => '15%');
        $listData['cols'][] = array('field' => 'summary', 'title' => '文章内容', 'width' => '20%');
        $listData['cols'][] = array('field' => 'category_name', 'title' => '文章分类', 'width' => '8%');
        $listData['cols'][] = array('field' => 'status_text', 'title' => '文章状态', 'width' => '8%');
        $listData['cols'][] = array('field' => 'create_time', 'title' => '创建时间', 'width' => '15%');
        $listData['cols'][] = array('fixed' => 'right', 'width' => '220', 'title' => '操作', 'toolbar' => '#rowBar');
        $this->displayList($listData);
    }

    public function del()
    {
        $id = I('id');
        $model = D('Article');
        $res = $model->where(['id' => ['in', $id]])->delete();
        if ($res) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    public function delAttach($id) {
        $model = D('Common/File');
        $map = ['id'=>$id,'from'];
    }
}
